package com.android.infantschool.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.ui.adapter.ImageAdapter;
import com.android.infantschool.ui.imageloader.PhotoViewPager;
import java.io.Serializable;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author cowards
 * created on 2019\3\4 0004
 **/
public class ImageActivity extends BaseActivity {

    private int position;
    private List<String> list;
    @BindView(R.id.tvTitle)
    public TextView tvTitle;
    @BindView(R.id.vpGallery)
    public PhotoViewPager mPhoto;
    @BindView(R.id.img_dismess)
    public ImageView mImgDismess;

    private String title;

    public static Intent newIntent(Context context, int position, List<String> list) {
        Intent intent = new Intent(context, ImageActivity.class);
        intent.putExtra("position", position);
        intent.putExtra("IMG_LIST", (Serializable) list);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        ButterKnife.bind(this);
        position = getIntent().getIntExtra("position", 0);
        list = getIntent().getStringArrayListExtra("IMG_LIST");
        initView();
    }

    public void initView() {
        title = "%1$d/%2$d";
        mImgDismess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageActivity.this.finish();
            }
        });
        tvTitle.setText(String.format(title, position + 1, list.size()));
        mPhoto.setAdapter(new ImageAdapter(list, ImageActivity.this));
        mPhoto.setCurrentItem(position);
        mPhoto.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int pos) {
                super.onPageSelected(pos);
                position = pos;
                tvTitle.setText(String.format(title, position + 1, list.size()));
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (list != null) {
            list.clear();
        }
    }
}